package com.amin.concurrent;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

	/**
	 * 闭锁 控制指定的多个线程全部执行完之后，再执行await()方法后的代码
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		CountDownLatch cdl =

		new CountDownLatch(2);
		new Thread(new BuyGuo(cdl)).start();
		new Thread(new BuyCai(cdl)).start();
		try {
			cdl.await();// 产生阻塞等待计数器的值变成0时继续执行
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("begin~~~~~~~~~~~~~~~~~~");
	}

}

class BuyGuo implements Runnable {
	private CountDownLatch cdl;

	public BuyGuo(CountDownLatch cdl) {
		this.cdl = cdl;
	}

	public void run() {
		System.out.println("hahahah");
		cdl.countDown();// 将计数器减1
	}
}

class BuyCai implements Runnable {
	private CountDownLatch cdl;

	public BuyCai(CountDownLatch cdl) {
		this.cdl = cdl;
	}

	public void run() {
		System.out.println("hehehehehe");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cdl.countDown();
	}

}
